package com.cqupt.slewing.room

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase

@Database(entities = [Item::class], version = 2, exportSchema = false)
abstract class ItemDatabase : RoomDatabase() {

    companion object {
        private var db: ItemDatabase? = null

        @Synchronized
        fun getDatabase(context: Context): ItemDatabase {
            if (db == null) {
                db = Room.databaseBuilder(
                    context.applicationContext,
                    ItemDatabase::class.java,
                    "item_database"
                )
                    .fallbackToDestructiveMigration()   //破坏式迁徙数据库
                    .build()
            }
            return db as ItemDatabase
        }
    }

    abstract fun getItemDao(): ItemDao
}